package com.Blog.www.service.Impl;


import com.Blog.www.dao.CommentDao;
import com.Blog.www.dao.Impl.CommentDaoImpl;
import com.Blog.www.dao.Impl.UserDaoImpl;
import com.Blog.www.dao.UserDao;
import com.Blog.www.po.BlogComment;
import com.Blog.www.po.CommentName;
import com.Blog.www.po.PageBean;
import com.Blog.www.po.User;
import com.Blog.www.service.CommentService;
import com.Blog.www.utils.DataExamine;

import java.util.ArrayList;
import java.util.List;

/**
 * 博客评论操作实现类
 *
 * @author 君未洋
 */
public class CommentServiceImpl implements CommentService {

    private static CommentDao dao = new CommentDaoImpl();
    private static DataExamine date = new DataExamine();
    private static UserDao userDao = new UserDaoImpl();


    @Override
    public boolean addComment(String bid, String uid, String comment) {
        //校验一下,再去调用dao方法
        if (bid == null || "".equals(bid) || uid == null || "".equals(uid) || comment == null || "".equals(comment) || date.numberExamine(bid) || date.numberExamine(uid)) {
            return false;
        } else {
            return dao.addComment(bid, uid, comment);
        }
    }

    @Override
    public List<BlogComment> getCommentList(int id, int prior) {
        if (id == 0) {
            return null;
        }
        return dao.getCommentList(id, prior);
    }

    @Override
    public PageBean<CommentName> getCommentList(String getCurrentPage, String getRows) {
        PageBean<CommentName> pb = new PageBean<CommentName>();
        if (getCurrentPage == null || "".equals(getCurrentPage) || date.numberExamine(getCurrentPage)) {
            getCurrentPage = "1";
        }
        if (getRows == null || "".equals(getRows) || date.numberExamine(getRows)) {
            getRows = "8";
        }
        int currentPage = Integer.parseInt(getCurrentPage);
        int rows = Integer.parseInt(getRows);
        if (currentPage <= 0) {
            currentPage = 1;
            //防止跳转到后面
        }
        int totalCount = dao.findCommentTotalCount(null);
        pb.setTotalCount(totalCount);
        int totalPage = (totalCount % rows) == 0 ? (totalCount / rows) : (totalCount / rows + 1);
        pb.setTotalPage(totalPage);
        if (currentPage > totalPage) {
            currentPage = totalPage;
        }
        pb.setCurrentPage(currentPage);
        pb.setRows(rows);

        int start = (currentPage - 1) * rows;
        List<BlogComment> list = dao.findCommentList(start, rows, null);
        if (list != null && list.size() != 0) {
            List<CommentName> commentNameList = new ArrayList<>();
            CommentName commentName = new CommentName();
            for (BlogComment comment : list) {
                commentName.setComment(comment);
                User user = userDao.getUser(comment.getUserid(), null);
                commentName.setUsername(null);
                if (user != null) {
                    commentName.setUsername(user.getUsername());
                }
                commentNameList.add(commentName);
            }
            pb.setList(commentNameList);
        }
        return pb;
    }

    @Override
    public boolean deleteComment(String cid) {
        if (cid == null || "".equals(cid) || date.numberExamine(cid)) {
            return false;
        } else {
            return dao.delete(cid);
        }
    }

    @Override
    public BlogComment getComment(int id) {
        if (id == 0) {
            return null;
        } else {
            return dao.getComment(id);
        }
    }

    @Override
    public boolean addReply(String bid, String uid, String cid, String comment) {
        if (bid == null || "".equals(bid) || uid == null || "".equals(uid) || cid == null || "".equals(cid) || comment == null
                || "".equals(comment) || date.numberExamine(bid) || date.numberExamine(cid)
                || date.numberExamine(uid)) {
            return false;
        } else {
            return dao.addReply(bid, uid, cid, comment);
        }
    }

    @Override
    public boolean deleteList(String[] commentIds) {
        if (commentIds == null || commentIds.length == 0) {
            return false;
        }
        boolean flag = false;
        for (String commentId : commentIds) {
            if (date.numberExamine(commentId)) {
                return false;
            }
            flag = dao.delete(commentId);
        }
        return flag;
    }


}
